import 'dart:math';

import 'package:flutter/material.dart';

import 'chart_bean.dart';

/// Base
class BasePainter extends CustomPainter {
  @override
  void paint(Canvas canvas, Size size) {}

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }

  /// 计算极值 最大值,最小值
  List<double> calculateMaxMin(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];
    double max = 0.0, min = 0.0;

    for (ChartBean bean in chatBeans) {
      if (max < bean.y) {
        max = bean.y;
      }

      if (min > bean.y) {
        min = bean.y;
      }
    }
    return [max, min];
  }

  List<double> calculateMaxMin2(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];

    List<double> y = [];
    for (ChartBean bean in chatBeans) {
      y.add(bean.y);
      y.add(bean.y2);
      y.add(bean.rate);
    }
    return [y.reduce(max), y.reduce(min)];
  }

  List<double> calculateRateMaxMin(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];
    double max = 0.0, min = 0.0;

    for (ChartBean bean in chatBeans) {
      if (max < bean.rate) {
        max = bean.rate;
      }

      if (min > bean.rate) {
        min = bean.rate;
      }
    }
    return [100, min];
  }

  List<double> calculateDoubleRateMaxMin(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];

    List<double> y = [];
    for (ChartBean bean in chatBeans) {
      y.add(bean.rate);
      y.add(bean.rate2);
    }
    return [y.reduce(max), y.reduce(min)];
  }

  List<double> calculateColumnMaxMin(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];

    List<double> y = [];
    for (ChartBean bean in chatBeans) {
      y.add(bean.y);
      y.add(bean.rate);
      y.add(bean.rate2);
    }
    return [y.reduce(max), y.reduce(min)];
  }

  List<double> calculateMaxMinDoubleBar(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return [0, 0];

    List<double> y = [];
    for (ChartBean bean in chatBeans) {
      y.add(bean.y);
      y.add(bean.y2);
    }
    return [y.reduce(max), y.reduce(min)];
  }

  Size calculateTextSize(String text, TextStyle style) {
    final TextPainter textPainter = TextPainter(
        text: TextSpan(text: text, style: style),
        maxLines: 1,
        textDirection: TextDirection.ltr)
      ..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.size;
  }

  /// 计算极值 最大值,最小值
  List<String> calculateXMaxMin(List<ChartBean>? chatBeans) {
    if (chatBeans == null || chatBeans.isEmpty) return ["", ""];
    int max = 0, min = 0;

    int indexMax = 0, indexMin = 0;
    for (var i = 0; i < chatBeans.length; i++) {
      if (max < chatBeans[i].x.length) {
        max = chatBeans[i].x.length;
        indexMax = i;
      }

      if (min > chatBeans[i].x.length) {
        min = chatBeans[i].x.length;
        indexMin = i;
      }
    }
    return [chatBeans[indexMax].x, chatBeans[indexMin].x];
  }
}
